package com.easybenefit.UUService.qrcode;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.widget.Toast;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2015/8/12.
 */
public class SFHCamera implements SurfaceHolder.Callback {
    private SurfaceHolder holder = null;
    private Camera mCamera;
    private int width, height;
    private Camera.PreviewCallback previewCallback;
    private boolean isrunEnCode;
    private boolean isrun;
    Context context;

    public SFHCamera(SurfaceHolder holder, int w, int h, Camera.PreviewCallback previewCallback, Context context) {
        this.holder = holder;
        this.holder.addCallback(this);
        this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        width = w;
        height = h;
        this.previewCallback = previewCallback;
        this.context = context;
        this.isrunEnCode = true;
        this.isrun = true;
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        try {
            if (mCamera != null) {
                initCamera();
                mCamera.startPreview();
                myThread.start();
                Log.e("Camera", "surfaceChanged");
            } else {
                Toast.makeText(context, "没有打开相机权限", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(context, "相机异常或没有权限", Toast.LENGTH_LONG).show();
        }
    }

    public void initCamera() {
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(width, height);//设置尺寸
        parameters.setPictureFormat(PixelFormat.JPEG);
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        parameters.setExposureCompensation(0);
        if (context.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)

        {
//如果是竖屏
            parameters.set("orientation", "portrait");
//在2.2以上可以使用
            mCamera.setDisplayOrientation(90);
        } else {
            parameters.set("orientation", "landscape");
//在2.2以上可以使用
//camera.setDisplayOrientation(0);
        }
        mCamera.setParameters(parameters);
        mCamera.startPreview();//开始预览
       mCamera.cancelAutoFocus();
    }

    private Thread myThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (isrun) {
                try {
                    if (isrunEnCode) {
                        Thread.sleep(1000);
                        if (mCamera != null)
                            mCamera.setOneShotPreviewCallback(previewCallback);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        try {
            mCamera = Camera.open(0);//启动服务
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(context, "没有打开相机的权限", Toast.LENGTH_LONG).show();
        }
        try {
            if (mCamera == null) {
                Toast.makeText(context, "没有打开相机的权限", Toast.LENGTH_LONG).show();
            } else {
                mCamera.setPreviewDisplay(holder);//设置预览
            }
            Log.e("Camera", "surfaceCreated");
        } catch (Exception e) {
//            holder.removeCallback(this);
//            mCamera.setPreviewCallback(null);
//            mCamera.stopPreview();
//            mCamera.release();//释放
//            mCamera = null;
//            Toast.makeText(context, "打开相机失败!请稍后重试", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }


    }

    /**
     * 开始解码
     */
    public void startEncode() {
        Log.e("start", "start MyThread");
        myThread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        try {
            if (mCamera != null) {
                isrun = false;
                isrunEnCode = false;
                holder.removeCallback(this);
                mCamera.setPreviewCallback(null);
                mCamera.stopPreview();
                mCamera.release();//释放
                mCamera = null;
            }
        } catch (Exception e) {

        }
    }

    /**
     * 取消解码
     */
    public void cancelEncode() {
        isrunEnCode = false;
    }

    /**
     * 恢复解码
     */
    public void resumeEncode() {
        isrunEnCode = true;
    }

    /**
     * 51.     * 自动对焦并回调Camera.PreviewCallback
     * 52.
     */

    public void AutoFocusAndPreviewCallback() {
        try {

            if (mCamera != null) {
                mCamera.autoFocus(mAutoFocusCallBack);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //   Toast.makeText(context, "打开相机失败!请稍后重试", Toast.LENGTH_SHORT).show();
        }
    }


    /**
     * 60.     * 自动对焦
     * 61.
     */

    private Camera.AutoFocusCallback mAutoFocusCallBack = new Camera.AutoFocusCallback() {

        @Override
        public void onAutoFocus(boolean success, Camera camera) {
            if (success) {  //对焦成功，回调Camera.PreviewCallback
//                mCamera.setOneShotPreviewCallback(previewCallback);
                initCamera();
                camera.cancelAutoFocus();
            }
        }
    };

    public Camera getMCamera() {
        return mCamera;
    }


}

